package com.zhang.member.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.exception.myexception.StateException;
import com.zhang.common.util.md5.Md5Util;
import com.zhang.common.util.network.UserAgentUtils;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.common.util.time.TimeUtils;
import com.zhang.member.dto.UserSearchDTO;
import com.zhang.member.entity.MemberUser;
import com.zhang.member.mapper.MemberUserMapper;
import com.zhang.member.service.IMemberUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前台会员用户表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-10-31
 */
@Service
public class MemberUserServiceImpl extends ServiceImpl<MemberUserMapper, MemberUser> implements IMemberUserService {

    @Override
    public R enroll(MemberUser user) {
        user.setCreateTime(TimeUtils.getLocalDateTimeStr());
        Map<String, String> md5 = Md5Util.getMD5Slat(user.getPassword());
        user.setMd5Salt(md5.get("slat"));
        user.setPassword(md5.get("value"));
        save(user);
        return RUtils.successMsg("注册成功");
    }

    @Override
    public R login(MemberUser user, HttpServletRequest httpServletRequest) {
        QueryWrapper<MemberUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", user.getUserName());
        MemberUser userInfo = getOne(wrapper);
        String userAgent = httpServletRequest.getHeader("user-agent");
        System.out.println("设备类型:" + UserAgentUtils.getDeviceType(userAgent));
        if (userInfo != null) {
            /*通过盐值+传入密码*/
            String pass = user.getPassword() + userInfo.getMd5Salt();
            String md5 = Md5Util.getMD5(pass);
            if (md5.equals(userInfo.getPassword())) {
                if (userInfo.getState() != 0) {
                    log.error("该用户被禁用");
                    throw new StateException("用户");
                }
                /*登录成功*/
                StpUtil.login(userInfo.getId(),"MOBILE");
                HashMap<String, String> map = new HashMap<>();
                map.put("token", StpUtil.getTokenValue());
                map.put("userName", userInfo.getUserName());
                map.put("avatar", userInfo.getAvatar().toString());
                map.put("userNickname", userInfo.getUserNickname());
                return RUtils.successData(map);
            }
        }
        return RUtils.err("用户名或者密码错误");
    }

    @Override
    public R getMyInfo() {
        long id = StpUtil.getLoginIdAsLong();
        MemberUser user = getById(id);
        if (user!=null){
            return RUtils.successData(user);
        }
        return RUtils.err("查询失败");
    }

    @Override
    public IPage<MemberUser> getMembers(UserSearchDTO userSearchDTO) {
        LambdaQueryWrapper<MemberUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.or().eq(userSearchDTO.getKeyWord() != null && !"".equals(userSearchDTO.getKeyWord())  , MemberUser::getId, userSearchDTO.getKeyWord())
                .or().eq(userSearchDTO.getKeyWord() != null && !"".equals(userSearchDTO.getKeyWord()), MemberUser::getUserName, userSearchDTO.getKeyWord())
                .eq(userSearchDTO.getState() != null, MemberUser::getState, userSearchDTO.getState())
                .eq(userSearchDTO.getGender() != null, MemberUser::getGender, userSearchDTO.getGender());
        return page(new Page<>(userSearchDTO.getPageNum(), userSearchDTO.getPageSize()), wrapper);
    }
}
